<?php
declare (strict_types=1);

namespace app\controller\home;

use app\common\controller\BaseController;
use think\App;
use think\facade\Cache;
use \Throwable;

class WxMessage extends BaseController
{
    protected $model;
    protected $appId;
    protected $appSecret;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->appId = config('wx.appId');
        $this->appSecret = config('wx.appSecret');
    }

    /**
     *
     * 预约提醒助教
     */
    public function book_to()
    {
        $openId = get_openid_by_id($this->memberId);
        $formId = input('form_id/d', 1);
        $template_id = config('wx_template.book_to');
        $content = [
            'thing1' => [
                'value' => '111111'
            ],
            'thing6' => [
                'value' => '111111'
            ],
            'date7' => [
                'value' => '111111111111'
            ],
            'name5' => [
                'value' => '111111'
            ]
        ];
        $this->sendTemplateMessage($openId, $formId, $template_id, $content);
    }

    /*
     *小程序模板消息
     *@param uid 用户id
     *$param template_id 模板id
     *@param form_id 表单提交场景下formId(只能用一次)
     *@param emphasis_keyword 消息加密密钥
    */
    public function sendTemplateMessage($openid, $form_id, $template_id, $content)
    {
        // 此openid为小程序的openid切勿与微信自动登录的openid混淆
        $xcx_open['openid'] = $openid;
        // openid可以通过PHP接口或者小程序获取
        if ($xcx_open['openid']) {
            $temp_msg = array(
                'touser' => "{$xcx_open['openid']}",
                'template_id' => "{$template_id}",
                'page' => "/pages/index/index",
                'form_id' => "{$form_id}",
                'data' => $content,
                'emphasis_keyword' => ""
            );
            $res = $this->sendXcxTemplateMsg(json_encode($temp_msg));
            dd($res);
        }
    }

    /**
     * 发送小程序模板消息
     * @param $data
     * [[@return](https://learnku.com/users/31554)](https://learnku.com/users/31554) array
     */
    public function sendXcxTemplateMsg($data, $appid = "", $appsecret = "")
    {
        // 具体模板格式参考公众平台申请的template_id
        if (!$appid || !$appsecret) {
            $appid = $this->appId;    //小程序id
            $appsecret = $this->appSecret;    //小程序秘钥
        }
        $access_token = $this->getXcxAccessToken($appid, $appsecret);
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
        return http_request($url, $data);
    }

    /**
     * 获取微信接口调用凭证
     * @param string $appid
     * @param string $appsecret
     * [[@return](https://learnku.com/users/31554)](https://learnku.com/users/31554) mixed
     */
    public function getXcxAccessToken($appid = '', $appsecret = '')
    {
        if (!$appid || !$appsecret) {
            $appid = $this->appId;
            $appsecret = $this->appSecret;
        }

        // 缓存获取
        $cache = new Cache();
        if (!$cache::get($appid . 'xcx_access_token')) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
            $res = http_request($url);
            // access_token设置到缓存中
            $cache::set($appid . 'xcx_access_token', $res['access_token'], 7000);
            return $res['access_token'];
        }

        return $cache::get($appid . 'xcx_access_token');
    }
}
